﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace DarwinIsDead.Traits
{
    public class Elusion : Trait
    {
        Player player;
        private float runSpeed;
        private float moveRange;
        private float direction;

        private Random random;

        // Ctor
        public Elusion(Zombie owner, World world = null)
            : base(owner, world)
        {
            random = new Random();
        }

        public override object Clone()
        {
            Elusion ret = new Elusion(Owner, World);
            return ret;
        }

        public override void Initialize()
        {
            // Put anything that couldn't be put into the constructor here.
            // That is, anything that depends on other Traits and couldn't be
            // initialized until the Zombie was fully formed.

            // Apply any changes to the owning Zombie that doesn't involve
            // accessing other Traits

            List<Entity> players = World.GetEntities<Player>();
            if (players.Count > 0)
            {
                player = (Player)players[0];
            }

            runSpeed = 50f; // Moves 50 pixels per second
            moveRange = 10;
            Level = 1;
        }

        protected override void AdjustLevel(int level)
        {
            // Apply any changes that occur with leveling up.

            moveRange += level/2;
        }

        protected override void InternalUpdate(GameTime gameTime)
        {
            // Our delta time (time between game updates) in seconds.
            // You can use whatever format you want, but this is my preferred.
            float dt = gameTime.ElapsedGameTime.Milliseconds / 1000f;
            int converted = (int)dt;

            // Move to player and hug the bastard
            if (player != null)
            {
                // Get the distance between the zombie and the player
                float dpx = player.Position.X - Owner.Position.X;
                float dpy = player.Position.Y - Owner.Position.Y;

                double dist = Math.Abs(dpx + dpy);
                if (dist > 100f && converted % 10 == 0)
                {
                    // Get the angle between the zombie and the player
                    direction = (float)Math.Atan2(dpy, dpx);
                    int randomX = random.Next(0, 100);
                    int randomY = random.Next(0, 100);
                    // Get our movement components
                    float dx = runSpeed * dt * (float)Math.Cos(direction);
                    float dy = runSpeed * dt * (float)Math.Sin(direction);

                    if (randomX % 2 == 0)
                    {
                        if (randomY % 2 == 0) {
                           Owner.Move(dx - moveRange, dy);
                        }
                        else {
                            Owner.Move(dx, dy - moveRange);
                        }
                    }
                    else
                    {
                        if (randomY % 2 == 0)
                        {
                            Owner.Move(dx + moveRange, dy);
                        }
                        else
                        {
                            Owner.Move(dx, dy + moveRange);
                        }
                    }
                }
            }
        }

        protected override void InternalDraw(SpriteBatch spriteBatch, Color color)
        {
            
        }
    }
}
